/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.coffeemaker.application.commands.generate;

import com.nanosn.coffeemaker.Project;
import com.nanosn.coffeemaker.application.commands.generate.util.ClassName;
import com.nanosn.coffeemaker.application.commands.generate.util.EntityClass;
import com.nanosn.coffeemaker.freemarker.FMPP;
import com.nanosn.util.Jar;
import com.nanosn.util.parsers.commandline.Command;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author selkhateeb
 */
public class RestAPI implements Command {

    private String getApplicationName(String[] args) throws Exception {
        if (args.length < 2) {
            throw new Exception("Application Name not specified");
        }
        return args[1].toLowerCase();
    }

    private ClassName[] getClassNames() throws Exception {

        Class[] classes = Project.getClassLoader().getModelClasses();

        ArrayList<ClassName> stringClasses = new ArrayList<ClassName>();

        for (Class class1 : classes) {
            stringClasses.add(new ClassName(class1.getSimpleName()));
        }

        return stringClasses.toArray(new ClassName[stringClasses.size()]);
    }

    @Override
    public void run(String[] args) throws Exception {
        /*
        +- rootPackage
        +- applicationName
        +- classNames
        +- directory
        +- Entities: ArrayList of EntityClass.getAsMap()
        |
        +- name: The name of the class.
        |    |
        |    +- single
        |    +- plural
        |
        +- idColumn: the id column
        |    |
        |    +- field
        |    |    |
        |    |    +- name
        |    |    +- type
        |    |
        |    +- getter
        |    +- setter
        |
        +- columns: ArrayList of columns
        |    |
        |    +- field
        |    +- getter
        |    +- setter
         */

        ArrayList<Map<String, Object>> entityClasses = new ArrayList<Map<String, Object>>();
        for (Class<?> clazz : Project.getClassLoader().getModelClasses()) {
            entityClasses.add(new EntityClass(clazz).getAsMap());
        }



        HashMap<String, Object> variables = new HashMap<String, Object>();
        String dir = Project.getSettings().getRootPackage() + "." + getApplicationName(args);
        variables.put("Entities", entityClasses);
        variables.put("classNames", getClassNames());
        variables.put("applicationName", getApplicationName(args));
        variables.put("directory", dir.replace(".", "/"));
        variables.put("rootPackage", Project.getSettings().getRootPackage());

        generateRest(variables);

    }

    private void generateRest(Map<String, Object> variables) throws IOException {
        final String projectDir = Project.PWD;

        String srcDir = Project.TEMPLATE_FILES_PACKAGE_DIRECTORY
                        + File.separator + "restful";

        if (!Project.getApplicationJar().isDirectory()) {

            String tmpDirectory = Project.TMP + Project.TEMPLATE_FILES_PACKAGE_DIRECTORY
                                  + File.separator + "restful";
            //Extract files to temp directory
            Jar.ExtractDirectory(Project.getApplicationJar(), srcDir, tmpDirectory);
            FMPP.RunFMPP(tmpDirectory, projectDir, variables);
        }
        else {
            FMPP.RunFMPP(srcDir, projectDir, variables);
        }
    }

    @Override
    public String getHelp() {
        return "Generates the REST api";
    }
}
